﻿using Home.Domain.Entities;
using Home.WebUI.Areas.Example.Controllers;
using Newtonsoft.Json;
using System.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utilities.ViewModel;
using Utilities;

namespace Home.WebUI.Controllers
{
    public class HomeController : Controller
    {
        #region 基本
        private EFDbContext db = new EFDbContext();
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.gongsi = db.SingleDatas.First(t => t.SingleName == "Company").Body;
            return View();
        }

        public ActionResult Culyure()
        {
            return View();
        }
        public ActionResult Company()
        {
            return View();
        }
        public ActionResult Services()
        {
            return View();
        }
        public ActionResult Contact_us()
        {
            return View();
        }
        [HttpPost]
        public ActionResult MessageBoard(MessageBoard mb)
        {
            if (string.IsNullOrWhiteSpace(mb.Body))
            {
                return Content(Common.MvcMessageBox("内容不能为空", "/home/Contact_us"));
            }
            mb.CreateDate = DateTime.Now;
            db.MessageBoards.Add(mb);
            db.SaveChanges();
            return Content(Common.MvcMessageBox("留言成功", "/home/Contact_us"));
        }

        public ActionResult Recruitment()//招聘
        {
            return View();
        }

        public ActionResult Success(int page = 1)//成功
        {
            //页面项数
            const int pageSize = 100;
            var list = db.Successes.Include(t => t.LeiBie).Include(t => t.Brand).OrderByDescending(c => c.CreateDate).Skip((page - 1) * pageSize).Take(pageSize);

            ViewData["PagingInfo"] = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = db.Successes.Count() };
            return View(list);
        }
        public ActionResult SuccessDetailed(int id)//成功
        {
            var model = db.Successes.Find(id);
            return View(model);
        }
        public ActionResult News(int page=1)
        {
            //页面项数
            const int pageSize = 12;
            var list = db.Bulletins.OrderByDescending(c => c.CreateDate).Skip((page - 1) * pageSize).Take(pageSize);
            ViewData["PagingInfo"] = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = db.Bulletins.Count() };
            return View(list);
        } 

        public ActionResult List()
        {
            return View();
        }
        public ActionResult NewsDetailed(int id)//成功
        {
            var model = db.Bulletins.Find(id);
            return View(model);
        }
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection form)
        {
            var adminName = Common.GetXmaData("AdminName");
            var passWord = Common.GetXmaData("AdminPassWord");
            if (adminName == form["adminname"].Trim() && passWord == Common.GetMD5(form["password"].Trim()))
            {
                Session["usermythi"] = 1;
                return Redirect("/Mythi/Home/index");
            }
            return Content(Common.MvcMessageBox("用户名或密码错误","/Home/Login"));
        }

        public ActionResult Test()
        {
            //var model = new Test1();
            ViewBag.json = new Test1().JsValidate;
            return View();
        }
        [HttpPost]
        public ActionResult Test22(Test1 model)
        {
            if (model.CheckAll(Request))
            {
                
            }
            else
            {
                return Content(model.ErrStr);
            }
            return View("Test");
        }

        public ActionResult Test1()
        {
            return View();
        }

        public ActionResult View1()
        {
            return View();
        }

        public ActionResult Ueditor()
        {
            return View();
        }

    }
}
